package easier.window.dialog.bottom.impl;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import easier.window.Item;
import easier.window.window.R;

/**
 * 底部弹出可选择的选择框,已经实现界面
 * 1.显示文字
 * 2.可配置文字选中颜色
 * 3.LinearLayoutManager
 * 4.拥有一个取消按钮
 */
public class SimpleBottomChooseDialogFragment extends BottomChooseDialogFragment< Item > {

    /* 设置文字颜色 */
    private @ColorInt int mCheckColor;
    private @ColorInt int mNormalColor;

    @Override
    protected View onCreateItemView( ViewGroup parent ) {

        return LayoutInflater.from( parent.getContext() ).inflate( R.layout.dialog_choose_item, parent, false );
    }

    @Override
    protected void onItemViewBind( View itemView, Item item, int position ) {

        if ( position == getModels().size() - 1 ) {
            itemView.findViewById( R.id.divider ).setVisibility( View.GONE );
        } else {
            itemView.findViewById( R.id.divider ).setVisibility( View.VISIBLE );
        }
        TextView textView = itemView.findViewById( R.id.text );
        textView.setText( item.getValue() );
        itemView.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                for ( Item checkItemModel : getModels() ) {
                    checkItemModel.setChecked( false );
                }
                item.setChecked( true );
                dismiss();
                onItemChecked( item );
            }
        } );

        if ( item.isChecked() ) {
            textView.setTextColor( mCheckColor );
        } else {
            textView.setTextColor( mNormalColor );
        }
    }

    /**
     * @param checkedColor 设置选中时颜色
     * @param normalColor  设置非选中时颜色
     */
    public void setTextColor( @ColorInt int checkedColor, @ColorInt int normalColor ) {

        mCheckColor = checkedColor;
        mNormalColor = normalColor;
    }
}
